from django.db import models
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
# 导入密码加密
from Utils import encrypt
from django.utils import timezone

# unique = True 唯一值
# blank = True 表示表单中的该字段可以不填（默认为False）
# null = True 表示该字段在数据库中可以为空
# db_index=True 创建索引

mobile_regex = RegexValidator(regex=r'^1[3-9]\d{9}$', message="请输入有效的11位手机号码")
pwd = encrypt.md5('x-admin')


# Create your models here.
class SA_Info(models.Model):
    """ SA 信息表 """
    Employee_No = models.CharField(verbose_name='员工号', max_length=12, unique=True)
    SA_CName = models.CharField(verbose_name='中文名', max_length=18, db_index=True)
    SA_EName = models.CharField(verbose_name='英文名', max_length=18, blank=True, null=True)
    Password = models.CharField(verbose_name='密码', max_length=128, default=pwd)
    Email = models.EmailField(verbose_name='邮件地址', max_length=128, unique=True)
    mobile = models.CharField(max_length=16, unique=True, verbose_name='手机号', validators=[mobile_regex])
    CSE_No = models.CharField(verbose_name='CSE_No', max_length=128, blank=True, null=True)
    RO = models.CharField(
        verbose_name='所属',
        max_length=4,
        choices=(
            ('ERO', 'ERO-东区'),
            ('SRO', 'SRO-南区'),
            ('WRO', 'WRO-西区'),
            ('NRO', 'NRO-北区'),
        ), default='ERO'
    )
    role = models.CharField(verbose_name='角色', max_length=10,
                            choices=(
                                ('user', 'USER'),
                                ('E_M', 'ERO Manager'),
                                ('S_M', 'SRO Manager'),
                                ('W_M', 'WRO Manager'),
                                ('N_M', 'NRO Manager'),
                                ('G_M', 'General Manager'),
                            ), default='user')
    City = models.CharField(verbose_name='城市', max_length=20, blank=True, null=True)
    Status = models.CharField(verbose_name='状态', max_length=6,
                              choices=(
                                  ('duty', '在职'),
                                  ('quit', '离职')
                              ), default='duty')

    class Meta:
        db_table = 'sa_info'
        verbose_name = 'SA人员信息'
        verbose_name_plural = verbose_name
        ordering = ['-SA_CName']

    def __str__(self):
        return self.SA_CName


class Customers(models.Model):
    """ 客户信息 """
    create_date = models.DateField(auto_now_add=True, verbose_name='创建日期')
    RO = models.CharField(
        verbose_name='区域',
        max_length=4,
        choices=(
            ('', '下拉选择'),
            ('ERO', 'ERO-东区'),
            ('SRO', 'SRO-南区'),
            ('WRO', 'WRO-西区'),
            ('NRO', 'NRO-北区'),
        ), default=''
    )
    name = models.CharField(verbose_name='客户名称', max_length=128, unique=True, db_index=True)
    status = models.CharField(
        verbose_name='状态',
        max_length=10,
        choices=(
            ('0', 'Enable'),
            ('1', 'Disable')
        ), default='0'
    )
    notes = models.TextField(verbose_name='备注', blank=True, null=True)

    class Meta:
        db_table = 'customers'
        verbose_name = '客户信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Project(models.Model):
    """ 项目信息 """
    SR_No = models.CharField(verbose_name='SR_No#', unique=True, max_length=24)
    RO = models.CharField(verbose_name='项目所属', max_length=30, null=True, blank=True)
    sa = models.ForeignKey(verbose_name='项目Owner', to=SA_Info, related_name='owners',
                           on_delete=models.SET_NULL, null=True, db_index=True)
    customer = models.ForeignKey(verbose_name='项目客户',
                                 to=Customers, on_delete=models.CASCADE, related_name='projects')
    category_choice = (
        ('故障排除', '故障排除'),
        ('新购实施', '新购实施'),
        ('添加设备', '设备添加'),
        ('添加模块', '模块添加'),
        ('变更需求', '需求变更'),
        ('升级版本', '版本升级'),
        ('系统迁移', '系统迁移'),
        ('系统变更', '系统变更'),
        ('技术咨询', '技术咨询'),
        ('应用开发', '应用开发'),
        ('项目经理', '项目经理'),
        ('色彩管理', '色彩管理'),
        ('其它应用', '其它应用'),
    )
    category = models.CharField(verbose_name='工作分类', max_length=20, choices=category_choice, default='故障排除')
    software_choice = (
        ('', '选择产品'),
        ('AWMS', 'AWMS Entry'),
        ('AWMS_O', 'AWMS Option'),
        ('AWMS_E', 'AWMS Enterprise'),
        ('司印', '司印'),
        ('畅印', '畅印'),
        ('AMDP', 'AMDP'),
        ('EQ', 'Equitrac'),
        ('ShareScan', 'Ecopy ShareScan'),
        ('PaperCut', 'PaperCut'),
        ('ATG', 'ATG Face'),
        ('SDCC', 'SDCC定制'),
        ('other', '其它产品'),
    )
    software = models.CharField(verbose_name='软件名称', max_length=20, choices=software_choice, default='')
    version = models.CharField(verbose_name='软件版本', max_length=10, null=True, blank=True)
    server_num = models.PositiveSmallIntegerField(verbose_name='服务器数量', default=0)
    mfd_num = models.PositiveSmallIntegerField(verbose_name='MFD数量', default=0)
    printer_num = models.PositiveSmallIntegerField(verbose_name='打印机数量', default=0)
    start_date = models.DateField(verbose_name='创建日期')
    end_date = models.DateField(verbose_name='结束日期', null=True, blank=True)
    status_choice = (
        ('Ongoing', '进行中'),
        ('Completed', '已完成'),
    )
    status = models.CharField(verbose_name='项目状态', max_length=15, choices=status_choice, default='Ongoing')
    actors = models.ManyToManyField(verbose_name='参与者', to=SA_Info, related_name='join_projects', blank=True)

    color_choice = (
        (1, "#56b8eb"),
        (2, '#f28033'),
        (3, '#ebc656'),
        (4, '#a2d148'),
        (5, '#20bfa4'),
        (6, '#7461c2'),
        (7, '#20bfa3'),
    )
    color = models.SmallIntegerField(verbose_name='Color', choices=color_choice, default=1)
    big_project = models.BooleanField(default=False, verbose_name='大项目')
    value = models.PositiveIntegerField(verbose_name='金额(K)', default=0)
    desc = models.TextField(verbose_name='需求描述')
    points = models.DecimalField(verbose_name='点数', max_digits=6, decimal_places=2, blank=True, default=0.00)

    class Meta:
        db_table = 'projects'
        verbose_name = '项目信息'
        verbose_name_plural = verbose_name


class Task(models.Model):
    """ task信息 """
    project = models.ForeignKey(verbose_name='所属项目', to=Project,
                                on_delete=models.SET_NULL, related_name='project_tasks', null=True)
    creator = models.ForeignKey(verbose_name='作者', to=SA_Info,
                                on_delete=models.SET_NULL, related_name='task_creator', null=True)
    sub_number_choice = (
        ('1', '1'),
        ('2', '2'),
        ('3', '3'),
        ('4', '4'),
        ('5', '5'),
        ('6', '6'),
        ('7', '7'),
        ('8', '8'),
        ('9', '9'),
        ('10', '10'),
    )
    sub_number = models.CharField(verbose_name='Sub_No#', max_length=4, choices=sub_number_choice, default='1')
    task_number = models.CharField(verbose_name='Task_No#', max_length=20, blank=True, null=True)
    type_choice = (
        ('故障排除', '故障排除'),
        ('新购实施', '新购实施'),
        ('添加设备', '添加设备'),
        ('添加模块', '模块添加'),
        ('变更需求', '需求变更'),
        ('升级版本', '版本升级'),
        ('系统迁移', '系统迁移'),
        ('系统变更', '系统变更'),
        ('技术咨询', '技术咨询'),
        ('应用开发', '应用开发'),
        ('项目经理', '项目经理'),
        ('色彩管理', '色彩管理'),
        ('其它应用', '其它应用'),
    )
    task_type = models.CharField(verbose_name='任务类型', choices=type_choice, max_length=20, default='故障排除')
    task_date = models.DateField(verbose_name='任务日期')
    start_time = models.TimeField(verbose_name='开始时间')
    end_time = models.TimeField(verbose_name='结束时间')
    overtime = models.PositiveSmallIntegerField(verbose_name='加班时间(分钟)', blank=True, default=0)
    task_server_num = models.PositiveSmallIntegerField(verbose_name='服务器数量', default=0)
    task_mfd_num = models.PositiveSmallIntegerField(verbose_name='MFD数量', default=0)
    task_printer_num = models.PositiveSmallIntegerField(verbose_name='打印机数量', default=0)
    # 取 project的software， 非显性
    software_type = models.CharField(verbose_name='软件类型', max_length=20, blank=True, null=True)
    total_time = models.PositiveIntegerField(verbose_name='工作总时间（分钟）', blank=True, default=0)
    content = models.TextField(verbose_name='工作内容记录')

    class Meta:
        db_table = 'task'
        verbose_name = '任务信息'
        verbose_name_plural = verbose_name


